//
//  XiangCeViewController.m
//  QT
//
//  Created by 甘世清 on 2025/1/8.
//

#import "XiangCeViewController.h"
#import "XiangCeCollectionViewCell.h"
#import "PhotoModel.h"
@interface XiangCeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end

@implementation XiangCeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"相册");
}

- (void)SQ_UIViewForSelfView{
    self.collectionView.backgroundColor = UIColor.whiteColor;
    [self.collectionView registerClass:[XiangCeCollectionViewCell class] forCellWithReuseIdentifier:@"XiangCeCollectionViewCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    [self SQ_collectViewAddRefresh];
    [self SQ_Header];
}
-(void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"9" forKey:@"size"];
    [HttpTool post:@"photo/list" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSArray * array = [PhotoModel mj_objectArrayWithKeyValuesArray:httpModel.data];
        [self.jsonArray removeAllObjects];
        PhotoModel * model = [PhotoModel new];
        model.isAdd = YES;
        [self.jsonArray addObject:model];
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
        [self.collectionView.mj_header endRefreshing];
    }];
}
- (void)SQ_Foother{
    [self.collectionView.mj_footer endRefreshing];
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.jsonArray.count > 9) {
        return 9;
    }
    return self.jsonArray.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    XiangCeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XiangCeCollectionViewCell" forIndexPath:indexPath];
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat w = SCREEN_WIDETH/3.f-10;
    return CGSizeMake(w, 220);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    if (model.isAdd) {
        [self SQ_addImage];
    } else {
        [self SQ_headerButtonClick:[self.jsonArray objectAtIndex:indexPath.item]];
    }
   
}

-(void)SQ_addImage{
    ImagePopView * vc = [[ImagePopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200+FootHeight)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    vc.page = 10 - self.jsonArray.count;
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc setAddImageArrayBlock:^(NSArray<UIImage *> * _Nonnull imageArray) {
        [wk_pop dismiss];
    }];
}

-(void)SQ_headerButtonClick:(PhotoModel*)model{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"操作") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"查看") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
    }];
    
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"删除") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
    }];
 
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
